package com.orhon.smartcampus.modules.systemctl.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.orhon.smartcampus.modules.systemctl.entity.Apis;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLApis;
import com.orhon.smartcampus.modules.systemctl.mm.ApisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.WidgetsDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.entity.Widgets;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLWidgets;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.WidgetsMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class WidgetsFetcher implements DataFetcher {

	@Autowired
	WidgetsDao widgetsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		List<GQLWidgets> GQLWidgets = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Widgets where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("module_id")!=null) {
				hql += "and module_id " + Util.decide(map.get("module_id"));
			}

			if(map.get("type")!=null) {
				hql += "and type "+ Util.decide(map.get("type"));
			}
			if(map.get("menu_id")!=null) {
				hql += "and menu_id " +  Util.decide(map.get("menu_id"));
			}
			if(map.get("client")!=null) {
				hql += "and clients " +  Util.decide(map.get("client"));
			}

			if(map.get("status")!=null) {
				hql += "and status " + Util.decide(map.get("status"));
			}
		}

		List<Widgets> list = widgetsDao.find(hql,null,page.getPage(),page.getLimit());


		for (Widgets item : list) {
			GQLMenus gqlMenus = null;
			if (env.getSelectionSet().contains("data/menu")){
				Menus menus = item.getSystemctlMenusByMenuId();
				gqlMenus = MenuMapper.MAPPER.toMenus(menus);
			}
			GQLModule gqlModule = null;
			if (env.getSelectionSet().contains("data/module")){
				Modules modules = item.getSystemctlModulesByModuleId();
				gqlModule = ModuleMapper.MAPPER.mapModule(modules);
			}

			List<GQLApis> GQLApis= null;
			if (env.getSelectionSet().contains("data/operationApiRelations")){
				List<Apis> lists = item.getApis();
				GQLApis = ApisMapper.MAPPER.toApisList(lists);
			}
			GQLWidgets widgets = WidgetsMapper.MAPPER.toWidgets(item);
			widgets.setMenu(gqlMenus);
			widgets.setModule(gqlModule);
			widgets.setApis(GQLApis);
			GQLWidgets.add(widgets);

		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = widgetsDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLWidgets).put("total", total);
	}


}
